using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUIMessageScroll : MonoBehaviour
	{
		public UIPanel scrollPanel;

		public UIScrollView scroll;

		public Transform messagesContainer;

		public FriendsUIChatMessage messageExample;

		public MyUIInput newMessageInput;

		public UIButton sendButton;

		private List<FriendsUIChatMessage> messages = new List<FriendsUIChatMessage>();

		private List<FriendsUIChatMessage> tmpMessages = new List<FriendsUIChatMessage>();

		private PlayerInfo currentPlayer = default(PlayerInfo);

		private float keyboardSize;

		private bool isKeyboardVisible;

		private Tweener scrollTweener;

		private void Awake()
		{
			messageExample.gameObject.SetActive(false);
		}

		private void OnEnable()
		{
			FriendsController.OnIdsListsUpdated += UpdateChat;
			MyUIInput myUIInput = newMessageInput;
			myUIInput.onKeyboardInter = (Action)Delegate.Combine(myUIInput.onKeyboardInter, new Action(SendNewMessage));
			MyUIInput myUIInput2 = newMessageInput;
			myUIInput2.onKeyboardVisible = (Action)Delegate.Combine(myUIInput2.onKeyboardVisible, new Action(OnKeyboardVisible));
			MyUIInput myUIInput3 = newMessageInput;
			myUIInput3.onKeyboardHide = (Action)Delegate.Combine(myUIInput3.onKeyboardHide, new Action(OnKeyboardHide));
			sendButton.isEnabled = true;
		}

		private void OnDisable()
		{
			FriendsController.OnIdsListsUpdated -= UpdateChat;
			MyUIInput myUIInput = newMessageInput;
			myUIInput.onKeyboardInter = (Action)Delegate.Remove(myUIInput.onKeyboardInter, new Action(SendNewMessage));
			MyUIInput myUIInput2 = newMessageInput;
			myUIInput2.onKeyboardVisible = (Action)Delegate.Remove(myUIInput2.onKeyboardVisible, new Action(OnKeyboardVisible));
			MyUIInput myUIInput3 = newMessageInput;
			myUIInput3.onKeyboardHide = (Action)Delegate.Remove(myUIInput3.onKeyboardHide, new Action(OnKeyboardHide));
		}

		public void OnKeyboardVisible()
		{
			if (!isKeyboardVisible)
			{
				isKeyboardVisible = true;
				keyboardSize = newMessageInput.heightKeyboard;
				if (Application.isEditor)
				{
					keyboardSize = 200f;
				}
				int num = Mathf.CeilToInt((keyboardSize + 82f) * 768f / (float)Screen.height);
				BCWDebug.Log(string.Format("Keyboard height is {0}. Screen height is {1}", keyboardSize, Screen.height));
				BCWDebug.Log(string.Format("New anchor setted to {0}", scrollPanel.bottomAnchor.absolute));
			}
		}

		public void OnKeyboardHide()
		{
			if (isKeyboardVisible)
			{
				isKeyboardVisible = false;
			}
		}

		public IEnumerator SetNewMessage(Message newMessage, bool tmpMessage)
		{
			FriendsUIChatMessage message = UnityEngine.Object.Instantiate(messageExample);
			message.transform.SetParent(messagesContainer.transform);
			message.transform.localScale = Vector3.one;
			message.transform.localPosition = Vector3.one * 10000f;
			message.SetMessageUI(newMessage.isMine, newMessage.text, newMessage.timeStamp, newMessage.messageId);
			message.gameObject.SetActive(true);
			if (tmpMessage)
			{
				tmpMessages.Insert(0, message);
			}
			else
			{
				messages.Insert(0, message);
			}
			yield return null;
			float currentOffset = 0f;
			foreach (FriendsUIChatMessage oldTmpMessage in tmpMessages)
			{
				oldTmpMessage.transform.localPosition = new Vector3(0f, currentOffset, 0f);
				currentOffset += (float)oldTmpMessage.myMessageBG.height;
			}
			foreach (FriendsUIChatMessage oldMessage in messages)
			{
				oldMessage.transform.localPosition = new Vector3(0f, currentOffset, 0f);
				currentOffset = ((!oldMessage.isMine) ? (currentOffset + (float)oldMessage.otherMessageBG.height) : (currentOffset + (float)oldMessage.myMessageBG.height));
			}
		}

		public void ShowChat(PlayerInfo playerInfo)
		{
			base.gameObject.SetActive(false);
			ClearMessages();
			ClearTmpMessages();
			currentPlayer.playerId = string.Empty;
			if (string.IsNullOrEmpty(playerInfo.playerId))
			{
				return;
			}
			currentPlayer = playerInfo;
			messagesContainer.transform.localPosition = Vector3.zero;
			base.gameObject.SetActive(true);
			foreach (Message message in FriendsController.instance.GetMessages(currentPlayer.playerId))
			{
				CoroutineRunner.Instance.StartCoroutine(SetNewMessage(message, false));
			}
			ScrollDown(0.1f);
		}

		public void UpdateChat()
		{
			if (string.IsNullOrEmpty(currentPlayer.playerId))
			{
				return;
			}
			List<Message> newMessages = FriendsController.instance.GetNewMessages(currentPlayer.playerId);
			ClearTmpMessages(newMessages);
			foreach (Message item in newMessages)
			{
				CoroutineRunner.Instance.StartCoroutine(SetNewMessage(item, false));
			}
			if (newMessages.Count > 0)
			{
				ScrollDown(0.1f);
			}
		}

		private void AddTmpMessage(string text, long messageid)
		{
			Message message = new Message();
			message.text = text;
			message.timeStamp = 0L;
			message.isMine = true;
			message.messageId = messageid;
			CoroutineRunner.Instance.StartCoroutine(SetNewMessage(message, true));
			ScrollDown(0.1f);
		}

		private void ClearMessages()
		{
			for (int i = 0; i < messages.Count; i++)
			{
				UnityEngine.Object.Destroy(messages[i].gameObject);
			}
			messages.Clear();
		}

		private void ClearTmpMessages()
		{
			for (int i = 0; i < tmpMessages.Count; i++)
			{
				UnityEngine.Object.Destroy(tmpMessages[i].gameObject);
			}
			tmpMessages.Clear();
		}

		private void ClearTmpMessages(List<Message> msgList)
		{
			foreach (Message msg in msgList)
			{
				int num = -1;
				for (int i = 0; i < tmpMessages.Count; i++)
				{
					if (msg.messageId == tmpMessages[i].messageId)
					{
						UnityEngine.Object.Destroy(tmpMessages[i].gameObject);
						num = i;
						break;
					}
				}
				if (num != -1)
				{
					tmpMessages.RemoveAt(num);
				}
			}
		}

		private IEnumerator CallWithDelay(Action action, float delay)
		{
			yield return new WaitForSeconds(delay);
			action();
		}

		public void SendNewMessage()
		{
			if (string.IsNullOrEmpty(currentPlayer.playerId) || string.IsNullOrEmpty(newMessageInput.value) || !sendButton.isEnabled)
			{
				return;
			}
			long num = PromoActionManager.CurrentUnixTimeSecond;
			string text = new ChatMessageChecker().Check(newMessageInput.value);
			if (FriendsController.instance.AddMessage(currentPlayer.playerId, text, num))
			{
				AddTmpMessage(text, num);
				sendButton.isEnabled = false;
				StartCoroutine(CallWithDelay(delegate
				{
					sendButton.isEnabled = true;
				}, 1f));
			}
			newMessageInput.value = string.Empty;
		}

		private void ScrollDown(float time)
		{
			CoroutineRunner.Instance.StartCoroutine(ScrollDownIE(time));
		}

		private IEnumerator ScrollDownIE(float time)
		{
			yield return null;
			AnimScroll(1f, time);
		}

		public void AnimScroll(float toParametr, float duration)
		{
			if (scrollTweener != null)
			{
				HOTween.Kill(scrollTweener);
				scrollTweener = null;
			}
			scrollTweener = HOTween.To(scroll.verticalScrollBar, duration, new TweenParms().Prop("value", toParametr));
		}
	}
}
